/*
 *
 *
 */

#include "PreGameState.h"

#ifndef _PREGAMESTATE_CPP_
#define _PREGAMESTATE_CPP_

#define TOTAL_TIME 13500

/*
 *
 */
void PreGameState::load(void)
{
    background = new Sprite("../Images/background_1.jpg",0,0);
    messageGet = new Text("Get","../Fonts/liberation_sans.ttf",640.0,30.0,TTF_STYLE_BOLD,255,0,0,80);
    messageNumber = new Text("100","../Fonts/liberation_sans.ttf",250.0,485.0,TTF_STYLE_BOLD,0,0,255,85);
    messageSmiley = new Text("Smileys","../Fonts/liberation_sans.ttf",-300.0,300.0,TTF_STYLE_BOLD,255,255,0,70);
    numbers = new Text("3","../Fonts/liberation_sans.ttf",280.0,180.0,TTF_STYLE_BOLD,255,0,0,140);
    messageYouHave = new Text("You Have","../Fonts/liberation_sans.ttf",-300.0,100.0,TTF_STYLE_BOLD,255,0,255,70);
    messageSec = new Text("60 Sec.","../Fonts/liberation_sans.ttf",640.0,300.0,TTF_STYLE_BOLD,0,100,255,80);;
    inputManager = Singleton<InputManager>::getInstance();
    timer.start();
}

/*
 *
 */
int PreGameState::update(int tDelta)
{
    inputManager->poolEvents(); /*Este método deve ser chamado no início do update de cada estado que utiliza a classe InputManager*/
    if(inputManager->isEventQuit())
        return -1;
    if(timer.getTime() < 4000)
    {
		SDL_Rect youHaveRect = messageYouHave->getRect();
		SDL_Rect secRect = messageSec->getRect();
		if(youHaveRect.x <= 120)
	    {
	        messageYouHave->updatePos( youHaveRect.x + tDelta*0.3, youHaveRect.y);
	    }
	    else if(secRect.x > 200)
	    {
	        messageSec->updatePos(secRect.x - tDelta*0.3,secRect.y);
	    }
	}
	else
	{
	    SDL_Rect getRect = messageGet->getRect();
	    SDL_Rect numRect = messageNumber->getRect();
	    SDL_Rect smileyRect = messageSmiley->getRect();
	    if(getRect.x > 200)
	    {
	        messageGet->updatePos(getRect.x - tDelta*0.3,getRect.y);
	    }
	    else if(numRect.y > 170)
	    {
	        messageNumber->updatePos(numRect.x,numRect.y - tDelta*0.2);
	    }
	    else if(smileyRect.x <= 120)
	    {
	        messageSmiley->updatePos( smileyRect.x + tDelta*0.3, smileyRect.y);
	    }
	}
    
    /*tempo para trocar o número*/
    if(timer.getTime() >= TOTAL_TIME - 3000)
        numbers->changeText("2");
    if(timer.getTime() >= TOTAL_TIME - 2000)
        numbers->changeText("1");
    if(timer.getTime() >= TOTAL_TIME - 1000)
    {
        numbers->changeText("Start");
        SDL_Rect numbersRect = numbers->getRect();
        numbers->updatePos( 130, numbersRect.y);
    }
        
    /*contagem de tempo para encerrar o estado*/
    if(timer.getTime() >= TOTAL_TIME)
        return 1;

    return 0;
}

/*
 *
 */
void PreGameState::draw(SDL_Surface *screen)
{
    background->render(screen);
    if(timer.getTime() < 4000)
    {
		messageYouHave->render(screen);
		messageSec->render(screen);
	}
    else if(timer.getTime() < TOTAL_TIME - 4000)
    {
        messageGet->render(screen);
        messageNumber->render(screen);
        messageSmiley->render(screen);
    }
    else
    {
        numbers->render(screen);
    }
        
    
}

/*
 *
 */
void PreGameState::unload(void)
{
    if(inputManager != NULL)
    {
        Singleton<InputManager>::releaseInstance();
    }
    if(messageSec != NULL)
    {
        delete messageSec;
        messageSec  = NULL;
    }
    if(messageYouHave != NULL)
    {
        delete messageYouHave;
        messageYouHave  = NULL;
    }
    if(numbers != NULL)
    {
        delete numbers;
        numbers  = NULL;
    }
    if(messageSmiley != NULL)
    {
        delete messageSmiley;
        messageSmiley  = NULL;
    }
    if(messageGet != NULL)
    {
        delete messageGet;
        messageGet  = NULL;
    }
    if(messageNumber != NULL)
    {
        delete messageNumber;
        messageNumber  = NULL;
    }
    if(background != NULL)
    {
        delete background;
        background = NULL;
    }
}

#endif
